/**
 * @FileName: CbcSubmit.java
 * @Author
 * @Description:
 * @Date 2016年5月9日 下午2:48:11
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.ccbpay;

import java.util.Map;

public class CcbSubmit{

    public static String buildRequest(Map<String, String> sPara) throws Exception{

        System.out.println(" CcbSubmit buildRequest ");
        if((sPara == null) || (sPara.size() == 0)){
            return "订单数据异常，提交支付失败";
        }

        String action = buildRequestPara(sPara);

        StringBuffer sbHtml = new StringBuffer();

        sbHtml.append("<form id=\"cbcsubmit\" name=\"cbcsubmit\" action=\"" + action + "\"" + "_input_charset=\""
                + CcbConfig.CHAR_SET + "\"" + " method=\"" + "post" + "\">");

        // submit按钮控件请不要含有name属性
        sbHtml.append("<input type=\"submit\" value=\"" + "" + "\" style=\"display:none;\"></form>");
        sbHtml.append("<script>document.forms[\"cbcsubmit\"].submit();</script>");

        return sbHtml.toString();
    }

    private static String buildRequestPara(Map<String, String> sPara) throws Exception{
        System.out.println("----------------url change -------------"+CcbConfig.CCB_URL);




        String posId = sPara.get(CcbConfig.Key.POSID);
        String pub32 = sPara.get(CcbConfig.Key.PUB);
        String remark1 = sPara.get(CcbConfig.Key.REMARK1);

        String gateway = sPara.get(CcbConfig.Key.GATEWAY);

        String orderId = sPara.get(CcbConfig.Key.ORDERID);
        String proInfo = sPara.get(CcbConfig.Key.PROINFO);
        String regInfo = sPara.get(CcbConfig.Key.REGINFO);
        String amount = sPara.get(CcbConfig.Key.PAYMENT);

        String str1 = "MERCHANTID=" + CcbConfig.MERCHANT_ID + "&POSID=" + posId + "&BRANCHID=" + CcbConfig.BRANCH_ID
                + "&ORDERID=" + orderId + "&PAYMENT=" + amount + "&CURCODE=" + CcbConfig.CURCODE + "&TXCODE="
                + CcbConfig.TXCODE + "&REMARK1=" + remark1 + "&REMARK2=" + CcbConfig.REMARK2 + "&TYPE=1";

        String strPub = "&PUB=" + pub32;

        String str2 = "&GATEWAY=" + gateway + "&CLIENTIP=" + CcbConfig.CLIENTIP + "&REGINFO=" + regInfo + "&PROINFO="
                + proInfo + "&REFERER=" + CcbConfig.MER_REFERER;

        String srcToSign = str1 + strPub + str2;

        String src = str1 + str2;

        String action = CcbConfig.CCB_URL + "?" + src + "&MAC=" + MD5.sign(srcToSign, CcbConfig.CHAR_SET);

        System.out.println("-----------------------------action url:" + action);

        return action;
    }

    // public static String buildRequest(ServiceType serviceType, CcbPos pos,
    // CcbOrder order){
    //
    // System.out.println(" CcbSubmit buildRequest ");
    // if((order == null) || (pos == null)){
    // return "订单数据异常，提交支付失败";
    // }
    //
    // String action = buildRequestPara(serviceType, pos, order);
    //
    // StringBuffer sbHtml = new StringBuffer();
    //
    // sbHtml.append("<form id=\"cbcsubmit\" name=\"cbcsubmit\" action=\"" +
    // action + "\"" + "_input_charset=\""
    // + CcbConfig.CHAR_SET + "\"" + " method=\"" + "post" + "\">");
    //
    // // submit按钮控件请不要含有name属性
    // sbHtml.append("<input type=\"submit\" value=\"" + "" + "\"
    // style=\"display:none;\"></form>");
    // sbHtml.append("<script>document.forms[\"cbcsubmit\"].submit();</script>");
    //
    // return sbHtml.toString();
    // }
    //
    // private static String buildRequestPara(ServiceType serviceType, CcbPos
    // pos, CcbOrder order){
    //
    // String remark1 = serviceType.getValue();
    //
    // String gateway = "";
    // if(order.getPayType() == PayType.UNION_PAY){
    // gateway = "UnionPay";
    // }
    //
    // String str1 = "MERCHANTID=" + CcbConfig.MERCHANT_ID + "&POSID=" +
    // pos.getPosId() + "&BRANCHID="
    // + CcbConfig.BRANCH_ID + "&ORDERID=" + order.getOrderId() + "&PAYMENT=" +
    // order.getPayAmount()
    // + "&CURCODE=" + CcbConfig.CURCODE + "&TXCODE=" + CcbConfig.TXCODE +
    // "&REMARK1=" + remark1 + "&REMARK2="
    // + CcbConfig.REMARK2 + "&TYPE=1";
    //
    // String strPub = "&PUB=" + pos.getPub32();
    //
    // String str2 = "&GATEWAY=" + gateway + "&CLIENTIP=" + CcbConfig.CLIENTIP +
    // "&REGINFO=" + order.getUserName()
    // + "&PROINFO=" + order.getProductName() + "&REFERER=" +
    // CcbConfig.MER_REFERER;
    //
    // String srcToSign = str1 + strPub + str2;
    //
    // String src = str1 + str2;
    //
    // String action = CcbConfig.CCB_URL + "?" + src + "&MAC=" +
    // MD5.sign(srcToSign, CcbConfig.CHAR_SET);
    //
    // System.out.println("action url:" + action);
    //
    // return action;
    // }

}
